from datetime import datetime
from django.db import models
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _
from tagging.models import Tag
from tagging.fields import TagField
import clapton.utils.markdown as markdown
import clapton.utils.publisher as publisher

class Channel(models.Model):
    site = models.ForeignKey(Site, verbose_name=_(u'Site'),) 
    name = models.CharField(_(u'Name'), max_length=250)
    slug = models.SlugField(_(u'Slug'), primary_key='True', unique=True,
                            help_text=_(u'Automatically built from the podcast name.'),)
    description = models.TextField(_(u'Description'), max_length=500,
	                                    blank=True, null=True,
	                                    help_text=_(u'Use <a href="http://daringfireball.net/projects/markdown/syntax">Markdown</a> syntax.'),)
 
    class Meta:
        verbose_name_plural = _(u'channel')	
	
    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return '/podcasts/%s/' % (self.slug)

class Podcast(models.Model):
    channel = models.ForeignKey(Channel, verbose_name=_(u'Channel'),)
    user = models.ForeignKey(User, verbose_name=_(u'User'),)
    content_type = models.CharField(max_length=5, blank=True, null=True)
    audio = models.FileField(upload_to='audio/%Y/%m/%d/', 
                                    verbose_name=_(u'Audio file'),
				    help_text=_(u'Select the podcast for upload.'),)
    title = models.CharField(_(u'Title'), max_length=80,)
    slug = models.SlugField(_(u'Slug'),
                            help_text=_(u'Automatically built from the title.'),)   
    summary = models.TextField(_(u'Summary'), blank=True, null=True, 
	                                help_text=_(u'Use <a href="http://daringfireball.net/projects/markdown/syntax">Markdown</a> syntax.'),)
    tags = TagField(verbose_name=_(u'Tags'), blank=True, null=True, 
	            help_text=_(u'Separate tags with spaces or commas.'))
    pub_date = models.DateTimeField(_(u'Date published'), 
                                    default=datetime.now, 
				    help_text=_(u'Publication date of the podcast.'),)
    publication_status = models.CharField(_(u'Publication Status'), 
	                                  max_length=32,
                                          choices=publisher.PUBLICATION_CHOICES,
					  default='Published',
					  help_text=_(u'Only published podcasts will appear on the site. Drafts will only be visible on the Administration page.'),)
    
    objects = models.Manager()
    published_objects = publisher.PublishedManager()

    class Meta:
        verbose_name_plural = _(u'podcast')
        ordering = ['-pub_date']
        get_latest_by = 'pub_date'

    def __unicode__(self):
        return self.title

    def save(self):
        self.content_type = self.audio[self.audio.rfind ('.')+1:]
        super(Podcast, self).save()
        Tag.objects.update_tags(self, self.tags)

    def get_absolute_url(self):
        return '/podcasts/%s/%s/%s/' % (self.channel.slug, self.pub_date.strftime("%Y/%b/%d").lower(),
                                        self.slug)
